<template>
  <div>
    <router-link to="/goods/add">
      <el-button type="primary">
        <i class="el-icon-circle-plus-outline"></i> 添加
      </el-button>
    </router-link>
    <div class="table-box">
      <el-table :data="list"  style="width: 100%">
        <el-table-column align="center" prop="firstcatename" label="一级分类" ></el-table-column>
        <el-table-column align="center"  prop="secondcatename" label="二级分类" ></el-table-column>
        <el-table-column align="center"  prop="goodsname" label="商品名称" ></el-table-column>
        <el-table-column align="center"  prop="role" label="商品图片" >
          <template slot-scope="scope">
              <img :src="$HOST+scope.row.img" style="width:100px;border:1px solid #eee;" alt="">
          </template>
        </el-table-column>
        <el-table-column align="center"  prop="price" label="销售价格" ></el-table-column>
        <el-table-column align="center"  prop="market_price" label="市场价格" ></el-table-column>
        <el-table-column align="center"  prop="isnew" label="是否新品" >
           <template slot-scope="scope">
               <el-switch v-model="scope.row.newboolean" @change="changeRow(scope.row,'isnew')"></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center"  prop="ishot" label="是否热卖" >
          <template slot-scope="scope">
              <el-switch v-model="scope.row.hotboolean"  @change="changeRow(scope.row,'ishot')"></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center"  prop="status" label="状态" >
          <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status">已启用</el-tag>
              <el-tag type="danger" v-else>已禁用</el-tag>  
          </template>
        </el-table-column>
        <el-table-column align="center"  label="操作" >
          <template slot-scope="scope">
            <router-link :to="'/goods/'+scope.row.id"><el-button type="success" circle size="small"> <i class="el-icon-edit"></i> </el-button></router-link>
            <el-button type="danger" size="small" @click="delRow(scope.row.id)" circle> <i class="el-icon-delete"></i> </el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <!-- 分页 -->
      <el-pagination
          background
          :page-size="size"
          @current-change="pagechange"
          :current-page="page"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getGoods,delGoods,getTotal,editGoods } from "@/request/goods"
export default {
    data(){
        return{
            list:[],
            total:0,  // 总数
            size:10,  // 每页条数
            page:1    // 页码数
        }
    },
    created(){
        this.getlist();  // 获取列表
    },
    methods:{
        async getlist(){
            let newlist  = await getGoods({size:this.size,page:this.page});
            this.list = newlist.map(val=>{
               val.newboolean =  val.isnew== 1 ? true:false;
               val.hotboolean =  val.ishot== 1 ? true:false;
               return val;
            }) 
            this.total = await getTotal(); 
        },
        delRow(id){  // 删除某一条数据，接收删除的ID
            // 弹框确认！
            this.$confirm('确定删除吗, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                // 删除功能！
                await delGoods(id);
                // 当前页数据只有一条的数据，删除成功之后，应该让页码退1，请求上一页的数据。
                if(this.list.length==1 && this.page!=1){
                    this.page--;
                }
                this.getlist();
            })
        },
        pagechange(val){
            this.page = val; // val就是新的页码
            this.getlist();  //  获取新的列表
        },
        async changeRow(val,type){ // val是这一行的数据，type是修改的字段（ishot,isnew）
          console.log(val,type)
            // { id:xxx,isnew:xxx }
            // { id:xxx,ishot:xxx }
            let newval = val[type] == 1 ? 0 : 1;
            let data = { id:val.id,[type]:newval }
            // 使用FormData进行提交
            let formdata = new FormData();
            for (let k in data) {
              formdata.append(k, data[k]);
            }
            await editGoods(formdata)
            this.getlist();
        }
    },
    components:{
    }
}
</script>
<style scoped>
</style>